/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   initmysqldb.cpp
 * Author: Administrator
 *
 * Created on 2018年7月18日, 上午11:18
 */
#include"PublicMySql.h"
#include<string.h>
#define SQL_CREATE_IfOctect_TABLE \
"CREATE TABLE `ifoctets_%s`(\
`ifCrc32` int(10) unsigned NOT NULL COMMENT '端口CRC32',\
`timestamp` int(11) NOT NULL COMMENT '时间戳',\
`ifHCInOctets` bigint(20) NOT NULL DEFAULT '0' COMMENT '入累计流量Byte',\
`ifHCOutOctets` bigint(20) NOT NULL DEFAULT '0' COMMENT '出累计流量Byte',\
`flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0数据入库,1手动添加',\
PRIMARY KEY (`timestamp`,`ifCrc32`)\
)ENGINE=MyISAM DEFAULT CHARSET=utf8;"

#define SQL_CREATE_IfFlow_TABLE \
"CREATE TABLE `ifflow_%s` (\
`ifCrc32` int(10) unsigned NOT NULL COMMENT '端口CRC32',\
`timestamp` INT(11) NOT NULL COMMENT '时间戳',\
`ifInFlow` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '入带宽Byte/S',\
`ifOutFlow` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '出带宽Byte/S',\
`flag` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '备用',\
PRIMARY KEY (`timestamp`,`ifCrc32`,`flag`)\
)ENGINE=MYISAM DEFAULT CHARSET=utf8;"

char*MakeCreateIfFlowTableSQL(const char*IP){
	char*sqlbuff=(char*)malloc(2048);
	memset(sqlbuff,0,2048);
	snprintf(sqlbuff,2048,SQL_CREATE_IfFlow_TABLE,IP);
	return sqlbuff;
}
char*MakeCreateIfOctetsTableSQL(const char*IP){
	char*sqlbuff=(char*)malloc(2048);
	memset(sqlbuff,0,2048);
	snprintf(sqlbuff,2048,SQL_CREATE_IfOctect_TABLE,IP);
	return sqlbuff;
}
void InitBaseDB(){
	MYSQL*mysql=OpenMySQL(MYSQL_BASE);
	const char*initsyssql="CREATE TABLE `sysinfo` (\
  `IP` char(16) NOT NULL COMMENT 'IP',\
  `ifNumber` smallint(6) DEFAULT NULL COMMENT '端口数量',\
  `sysName` varchar(255) DEFAULT NULL COMMENT '主机名',\
  `sysObjectID` varchar(255) DEFAULT NULL COMMENT '系统OID',\
  `sysContact` varchar(255) DEFAULT NULL COMMENT '联系信息',\
  `sysLocation` varchar(255) DEFAULT NULL COMMENT '物理位置',\
  `sysDescr` varchar(255) DEFAULT NULL COMMENT '系统描述',\
  `sysServices` tinyint(4) DEFAULT NULL COMMENT '服务类型',\
  `ipForwarding` tinyint(4) DEFAULT NULL COMMENT '转发功能',\
  `sysUpTimeInstance` int(11) unsigned DEFAULT NULL COMMENT '运行时间',\
  `snmptime` smallint(6) DEFAULT NULL COMMENT 'SNMP延时(ms)',\
  `pingtime` smallint(6) DEFAULT NULL COMMENT 'PING延时(ms)',\
  `updatetime` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '更新时间',\
  `enable` tinyint(4) NOT NULL DEFAULT 1 COMMENT '0禁用,1起用',\
  `Manufacturer` varchar(16) DEFAULT NULL COMMENT '生产厂商',\
  `ProductName` varchar(32) DEFAULT NULL COMMENT '产品型号',\
  `softVersion` varchar(64) DEFAULT NULL COMMENT '软件版本',\
  PRIMARY KEY (`IP`)\
) ENGINE=MyISAM DEFAULT CHARSET=utf8\
";
	const char*initifsql="CREATE TABLE `iftable` (\
  `IP` char(16) NOT NULL,\
  `ifIndex` int(11) DEFAULT NULL,\
  `ifCrc32` int(10) unsigned DEFAULT NULL,\
  `ifDescr` varchar(32) NOT NULL,\
  `ifFullDescr` varchar(64) DEFAULT NULL,\
  `ifAlias` varchar(255) DEFAULT NULL,\
  `ifName` varchar(255) DEFAULT NULL,\
  `ifType` smallint(6) DEFAULT NULL,\
  `ifTypeDescr` varchar(64) DEFAULT NULL,\
  `ifMtu` smallint(6) DEFAULT NULL,\
  `ifSpeed` mediumint(9) DEFAULT NULL,\
  `ifPhysAddress` char(18) DEFAULT NULL,\
  `ifAdminStatus` tinyint(4) DEFAULT NULL,\
  `ifOperStatus` tinyint(4) DEFAULT NULL,\
  `ifLastChange` int(11) DEFAULT NULL,\
  `ifConnectorPresent` tinyint(4) DEFAULT NULL,\
  `ifCounterDiscontinuityTime` int(11) DEFAULT NULL,\
  `uptime` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),\
  PRIMARY KEY (`IP`,`ifDescr`),\
  UNIQUE KEY `ifIndex` (`IP`,`ifIndex`)\
) ENGINE=MyISAM DEFAULT CHARSET=utf8\
";
	RunMySQL(mysql,initsyssql);
	RunMySQL(mysql,initifsql);
	CloseMySQL(mysql);
}
void InitFlowDB(){
	MYSQL*mysql=OpenMySQL(MYSQL_FLOW);
	char*initifoctetssql=MakeCreateIfOctetsTableSQL("0.0.0.0");
	char*initifflowsql=MakeCreateIfFlowTableSQL("0.0.0.0");
	RunMySQL(mysql,initifoctetssql);
	RunMySQL(mysql,initifflowsql);
	free(initifoctetssql);
	free(initifflowsql);
	CloseMySQL(mysql);
}
void InitMySQLDb(){
	InitBaseDB();
	InitFlowDB();
	return ;
}

